<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属阶段" prop="stageName">
        <el-select v-model="queryParams.stageName" placeholder="请选择所属阶段" clearable>
          <el-option
            v-for="dict in dict.type.sys_stage"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务要求" prop="taskType">
        <el-select v-model="queryParams.taskType" placeholder="请选择任务要求" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_requirements"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskCategory">
        <el-select v-model="queryParams.taskCategory" placeholder="请选择任务类型" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优先级" prop="taskPriority">
        <el-select v-model="queryParams.taskPriority" placeholder="请选择优先级" clearable>
          <el-option
            v-for="dict in dict.type.ya_task_priority"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:stage_tasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['teaching:stage_tasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['teaching:stage_tasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:stage_tasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stage_tasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属阶段" align="center" prop="stageName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_stage" :value="scope.row.stageName"/>
        </template>
      </el-table-column>
      <el-table-column label="任务编号" align="center" prop="taskCode" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务要求" align="center" prop="taskType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_requirements" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" align="center" prop="taskCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_type" :value="scope.row.taskCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="taskPriority">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ya_task_priority" :value="scope.row.taskPriority"/>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.taskStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="任务描述" align="center" prop="taskDescription" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:stage_tasks:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewProgress(scope.row)"
            v-hasPermi="['teaching:stage_tasks:view']"
          >查看任务完成情况</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:stage_tasks:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 查看任务进度对话框 -->
    <el-dialog title="任务完成情况" :visible.sync="openProgress" width="80%" append-to-body>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="选择班级">
          <el-select v-model="selectedClassId" placeholder="请选择班级" @change="getStudentTasks">
            <el-option
              v-for="item in classesList"
              :key="item.id"
              :label="item.className"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="studentTasks">
        <el-table-column label="学生姓名" prop="name" align="center" />
        <el-table-column label="完成情况" prop="submitStatus" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.submitStatus === null || scope.row.submitStatus === 0">未提交</span>
            <span v-else>已提交</span>
          </template>
        </el-table-column>
        <el-table-column label="成果" prop="attachment" align="center">
          <template slot-scope="scope">
          <a v-if="scope.row.attachment" :href="scope.row.attachment" target="_blank">
            <image-preview :src="fileIcon" alt="File" :width="50" :height="50"/>
          </a>
          <span v-else>未上传</span>
        </template>
        </el-table-column>
        <el-table-column label="提交时间" prop="submitTime" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.submitTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="检查情况" prop="feedback" align="center">
          <template slot-scope="scope">
            <el-select v-model="scope.row.feedback" placeholder="请选择检查情况" @change="handleFeedbackChange(scope.row)">
              <el-option
                v-for="dict in dict.type.ya_task_qualified"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属阶段" prop="stageName">
          <el-select v-model="form.stageName" placeholder="请选择所属阶段">
            <el-option
              v-for="dict in dict.type.sys_stage"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务编号" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入任务编号" />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务要求" prop="taskType">
          <el-select v-model="form.taskType" placeholder="请选择任务要求">
            <el-option
              v-for="dict in dict.type.ya_task_requirements"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务类型" prop="taskCategory">
          <el-select v-model="form.taskCategory" placeholder="请选择任务类型">
            <el-option
              v-for="dict in dict.type.ya_task_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先级" prop="taskPriority">
          <el-select v-model="form.taskPriority" placeholder="请选择优先级">
            <el-option
              v-for="dict in dict.type.ya_task_priority"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-radio-group v-model="form.taskStatus">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务描述" prop="taskDescription">
          <el-input v-model="form.taskDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStudent_tasks,
  listStage_tasks,
  getStage_tasks,
  delStage_tasks,
  addStage_tasks,
  updateStage_tasks,
  updateStudent_tasks
} from "@/api/teaching/stage_tasks";
import { listClass } from "@/api/teaching/class";
import fileIcon from '@/assets/images/file.png';


export default {
  name: "Stage_tasks",
  dicts: ['sys_stage', 'ya_task_requirements', 'ya_task_type', 'sys_normal_disable', 'ya_task_priority', 'ya_task_qualified'],
  data() {
    return {
      // 文件图片
      fileIcon: fileIcon,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 进度弹窗相关
      openProgress: false,
      classesList: [],      // 班级列表
      studentTasks: [],     // 学生任务列表
      selectedClassId: null, // 选中的班级ID
      taskId: null,         // 当前任务ID
      feedback: null,       // 当前任务反馈
      submitStatus: null,   // 当前任务提交状态
      studentId:  null,     // 当前学生ID
      // 任务表格数据
      stage_tasksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stageName: null,
        taskCode: null,
        taskName: null,
        taskType: null,
        taskCategory: null,
        taskPriority: null,
        taskStatus: null,
        taskDescription: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stageName: [
          { required: true, message: "所属阶段不能为空", trigger: "change" }
        ],
        taskCode: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务要求不能为空", trigger: "change" }
        ],
        taskCategory: [
          { required: true, message: "任务类型不能为空", trigger: "change" }
        ],
        taskPriority: [
          { required: true, message: "优先级不能为空", trigger: "change" }
        ],
        taskStatus: [
          { required: true, message: "任务状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 更新检查情况操作 */
    handleFeedbackChange(row){
      this.studentId = row.studentId;
      this.feedback = row.feedback;
      this.submitStatus = row.submitStatus;
      const params = { taskId: this.taskId, feedback: this.feedback, submitStatus: this.submitStatus, studentId: this.studentId }
      updateStudent_tasks(params).then(response => {
        if (response.code === 200) {
          this.$message({
            message: '检查成功',
            type: 'success'
          });
        }
      });
    },
    /** 查看进度按钮操作 */
    handleViewProgress(row) {
      this.openProgress = true;
      this.taskId = row.id;
      // 获取班级列表
      listClass().then(response => {
        this.classesList = response.rows;
        // 点击按钮后直接查询所有班级的学生任务列表
        this.getStudentTasks();
      });
    },
    /** 获取学生任务完成任务情况列表 */
    getStudentTasks() {
      this.loading = true;
      const params = this.selectedClassId ? { taskId: this.taskId, classesId: this.selectedClassId } : { taskId: this.taskId, classesId: null };
      listStudent_tasks(params).then(response => {
        this.studentTasks = response;
        this.loading = false;
      });
    },
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      listStage_tasks(this.queryParams).then(response => {
        this.stage_tasksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stageName: null,
        taskCode: null,
        taskName: null,
        taskType: null,
        taskCategory: null,
        taskPriority: null,
        taskStatus: null,
        taskDescription: null,
        //attachments: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStage_tasks(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStage_tasks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStage_tasks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delStage_tasks(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/stage_tasks/export', {
        ...this.queryParams
      }, `stage_tasks_${new Date().getTime()}.xlsx`)
    },
    // 取消按钮1
    cancel1() {
      this.openProgress = false;
    },
  }
};
</script>
